package com.milo.easypoi.controller.export;

import com.milo.easypoi.util.ExportWordUtil;
import com.milo.easypoi.util.MoneyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author Milo Lee
 * @date 2022-09-05 10:33
 */
@Controller
@Slf4j
public class WordController {


    /**
     *  根据word模板导出报告
     * @author Milo Lee
     * @date 2022/9/5 11:38
     * @param request
     * @param response
     * <p>
     *     一.准备模板
     *     二.里面EasyPoi动态填充数据
     *     三.导出报告
     * </p>
     */
    @RequestMapping(value = "/wordTemplateExport", method = RequestMethod.GET)
    public void getZbrzWord(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        //word模板地址
        String wordModelPath = "F:/wordTemplate/ceshi2.docx";
        //数据预处理
        Double money = 3400.00D;
        String money_total = MoneyUtils.change(money);
        Calendar now = Calendar.getInstance();
        //生成临时文件地址
        String filePath = "F:\\Download\\";
        //装填参数
        params.put("order_number", "20210408");
        params.put("money_total", money_total);
        params.put("order_money", "3400");
        params.put("y", now.get(Calendar.YEAR));
        params.put("m", (now.get(Calendar.MONTH) + 1));
        params.put("d", now.get(Calendar.DAY_OF_MONTH));
        List<Map<String, Object>> dataList = new ArrayList<>();
        //动态填充数据
        for (int i = 0; i < 2; i++) {
            Map<String, Object> data = new HashMap<>();
            data.put("index", i);
            data.put("title", "测试" + i);
            data.put("product_description", "单位" + i);
            data.put("buy_num", "数量" + i);
            data.put("saleprice", "单价" + i);
            data.put("buy_price", "总金额" + i);
            dataList.add(data);
        }
        params.put("dataList", dataList);
        ExportWordUtil.exportWord(wordModelPath, filePath, "wordExport.docx", params, request, response);
    }




}
